package cn.virens.common;

import java.nio.ByteBuffer;

public class BytesBuffer {
	private final ByteBuffer buffer;

	private int mark = -1;

	private BytesBuffer(ByteBuffer buffer) {
		this.buffer = buffer;
	}

	public static BytesBuffer of(ByteBuffer buffer) {
		return new BytesBuffer(buffer);
	}

	public int mark() {
		this.mark = buffer.position();

		return buffer.remaining();
	}

	public int reset(int offset) {
		if (this.mark >= 0 && buffer.remaining() > offset) {
			this.buffer.position(mark + offset);
		}

		return buffer.remaining();
	}

	public byte get() {
		return buffer.get();
	}

	public byte get(int offset) {
		return buffer.get(pos(offset));
	}

	public short getShort() {
		return buffer.getShort();
	}

	public short getShort(int offset) {
		return buffer.getShort(pos(offset));
	}

	public int position() {
		return buffer.position();
	}

	public int remaining() {
		return buffer.remaining();
	}

	private int pos(int offset) {
		return buffer.position() + offset;
	}
}
